<?php
namespace app\index\logic;

use think\Model;
use app\admin\model\MemberModel;

/**
 * 登录注册
 * @author Alinejun666
 * Date 2018-8-22
 */
class LoginLogic extends Model
{
    public function regist($data)
    {
        $member = new MemberModel();
        // 规则验证
        if(preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$data['nickname'])){
            return json_encode(msg_error("昵称包含非法字符！"));
        }
        if(preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$data['username'])){
            return json_encode(msg_error("用户名包含非法字符！"));
        }
        if(preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$data['password'])){
            return json_encode(msg_error("密码包含非法字符！"));
        }

        // 数据验证
        $where  = ['username' => $data['username']];
        $count  = $member->get_count($where);
        if ($count > 0) return json_encode(msg_error("用户名已存在！"));
        
        $where  = ['nickname' => $data['nickname']];
        $count  = $member->get_count($where);
        if ($count > 0) return json_encode(msg_error("昵称已存在！"));

        // 添加会员
        $password  = md5($data['password']."sanw666");
        $regist_ip = get_client_ip();
        $headpic = "./public/static/index/images/headpic/temp".rand(1,10).".jpg";

        $where = [
            'nickname'    => $data['nickname'],
            'username'    => $data['username'],
            'password'    => $password,
            'headpic'     => $headpic,
            'regist_ip'   => $regist_ip,
            'regist_time' => date("Y-m-d H:i:s", time()),
        ];
        $res = $member->insert_data($where);
        if ($res) {
            return json_encode(msg_success("注册成功！"));
        }
    }

    public function login($data)
    {
        $member = new MemberModel();
        // 规则验证
        if(preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$data['nickname'])){
            return json_encode(msg_error("昵称包含非法字符！"));
        }

        if(preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$data['password'])){
            return json_encode(msg_error("密码包含非法字符！"));
        }

        // 验证用户名
        $where = [
            'username' => $data['username'],
        ];
        $res = $member->get_count($where);
        if (!$res) return json_encode(msg_error("用户名不存在"));

        // 验证密码
        $where['password'] = md5($data['password']."sanw666");
        $res = $member->get_count($where);
        if (!$res) return json_encode(msg_error("密码错误"));

        // 验证状态
        $where['status'] = 1;
        $info = $member->get_info($where);
        if (empty($info)) {
            return json_encode(msg_error("该用户已被禁止"));
        }

        // 更新时间
        $wherearr = [
            'user_id'    => $info['user_id'],
            'last_login' => date("Y-m-d H:i:s",time()),
        ];
        $member->save_data($wherearr);

        unset($info['password']);
        unset($info['status']);
        
        // 记录时间
        $info['login_time']  = date("Y-m-d H:i:s",time());
        $info['active_time'] = date("Y-m-d H:i:s",time());
        session('member', null);
        session('member', $info);

        echo json_encode(msg_success("登录成功"));
        exit;

    }

	public function verify()
    {
        $captcha = new Captcha();
        return $captcha->entry();    
    }
}
